﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Concurrent;
using System.Threading;
using System.ServiceModel;
using CRM.Index.Box;
using CRM.Index.IProvider;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Entity.Page;

namespace CRM.Index.Provider
{
   
    public partial class ProviderService : IProviderService
    {
       public static ReaderWriterLockSlim  articleInfo_rws = new ReaderWriterLockSlim();
       
        #region 插入操作
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="ArticleInfo"></param>
        public bool ArticleInfo_Insert(ArticleInfo articleInfo)
        {
          try
            {
                articleInfo_rws.EnterWriteLock();
                
                DictionaryBox.articleInfoDictionary.Add(articleInfo.ArticleId, articleInfo);

                DictionaryBox.articleInfoArticleIdSort.Add(articleInfo.ArticleId);
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                articleInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 更新操作
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="ArticleInfo"></param>
        public bool ArticleInfo_Update(ArticleInfo articleInfo)
        {
          try
            {
                articleInfo_rws.EnterWriteLock();

                DictionaryBox.articleInfoDictionary[articleInfo.ArticleId] = articleInfo;
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                articleInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="ArticleInfo"></param>
        public bool ArticleInfo_Delete(ArticleInfo articleInfo)
        {
          try
            {
                articleInfo_rws.EnterWriteLock();

                DictionaryBox.articleInfoDictionary.Remove(articleInfo.ArticleId);

                DictionaryBox.articleInfoArticleIdSort.Remove(articleInfo.ArticleId);
                
                return true;
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
            finally
            {
                articleInfo_rws.ExitWriteLock();
            }
        }
        #endregion
        
        #region 根据主键获取一条
        /// <summary>
        /// 根据主键获取一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ArticleInfo ArticleInfo_Single(Int64 id)
        {
            try
            {
                articleInfo_rws.EnterReadLock();

                return DictionaryBox.articleInfoDictionary[id];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleInfo_rws.ExitReadLock();
            }
        }
        #endregion
        
        #region 获取TopK条
        /// <summary>
        /// 获取TopK条
        /// </summary>
        /// <param name="top"></param>
        public ArticleInfoPage ArticleInfo_Top(int top)
        {
            try
            {
                 articleInfo_rws.EnterReadLock();

                var  articleInfoPage = new  ArticleInfoPage(){ ListEntity = new List<ArticleInfo>() };

                var skip = DictionaryBox.articleInfoArticleIdSort.Count - top < 0 ? 0 : DictionaryBox.articleInfoArticleIdSort.Count - top;

                var listID = DictionaryBox.articleInfoArticleIdSort.Skip(skip).Take(top).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    articleInfoPage.ListEntity.Add(DictionaryBox.articleInfoDictionary[listID[i]]);
                }

                articleInfoPage.RecordCount = DictionaryBox.articleInfoArticleIdSort.Count;

                return articleInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                 articleInfo_rws.ExitReadLock();
            }
        }
        #endregion

        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ArticleInfoPage ArticleInfo_Page(PageInfo info)
        {
            try
            {
                articleInfo_rws.EnterReadLock();

                var articleInfoPage = new ArticleInfoPage() { ListEntity = new List<ArticleInfo>() };

                var skip = DictionaryBox.articleInfoArticleIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.articleInfoArticleIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    articleInfoPage.ListEntity.Add(DictionaryBox.articleInfoDictionary[listID[i]]);
                }

                articleInfoPage.RecordCount = DictionaryBox.articleInfoArticleIdSort.Count;

                return articleInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleInfo_rws.ExitReadLock();
            }
        } 
        #endregion
        
        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ArticleInfoExtraPage ArticleInfoExtra_Page(PageInfo info,int articleType)
        {
            try
            {
                articleInfo_rws.EnterReadLock();

                var articleInfoExtaPage = new ArticleInfoExtraPage() { ListEntity = new List<ArticleInfoExtra>() };

                var articleList = DictionaryBox.articleInfoDictionary.Values.Where(t => t.ArticleType == articleType).OrderByDescending(t => t.ArticleId).ToList();

                //var skip = DictionaryBox.articleInfoArticleIdSort.Count - (info.PageIndex * info.PageSize);
                var skip = (info.PageIndex - 1) * info.PageSize;

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var list = articleList.Skip(skip).Take(info.PageSize).ToList();

                //var listID = DictionaryBox.articleInfoArticleIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    var articleInfoExtra = new ArticleInfoExtra()
                    {
                        ArticleId = list[i].ArticleId,
                        Title = list[i].Title,
                        ArticleType = list[i].ArticleType,
                        Image = list[i].Image,
                        ArticleContent = list[i].ArticleContent,
                        CreateTime = list[i].CreateTime,
                        CreateUser = list[i].CreateUser,
                        ClickCount = list[i].ClickCount,
                        Disable = list[i].Disable,
                        IsNew = list[i].IsNew,
                        IsTop = list[i].IsTop,
                    };

                    articleInfoExtaPage.ListEntity.Add(articleInfoExtra);                    
                }
                articleInfoExtaPage.RecordCount = articleList.Count;

                //for (int i = listID.Count - 1; i >= 0; i--)
                //{
                //    var single = DictionaryBox.articleInfoDictionary[listID[i]];

                //    var articleInfoExtra = new ArticleInfoExtra()
                //    {
                //        ArticleId=single.ArticleId,
                //        Title=single.Title,
                //        ArticleType=single.ArticleType,
                //        Image=single.Image,
                //        ArticleContent=single.ArticleContent,
                //        CreateTime=single.CreateTime,
                //        CreateUser=single.CreateUser,
                //        ClickCount=single.ClickCount,
                //        Disable=single.Disable,
                //        IsNew=single.IsNew,
                //        IsTop=single.IsTop,
                //    };

                //    articleInfoExtaPage.ListEntity.Add(articleInfoExtra);
                //}

                //articleInfoExtaPage.RecordCount = DictionaryBox.articleInfoArticleIdSort.Count;

                return articleInfoExtaPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleInfo_rws.ExitReadLock();
            }
        }
        #endregion
    }
}
